<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>设备管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="${#request.getContextPath()}+'/static/layui/css/layui.css'" media="all">
    <link rel="stylesheet" th:href="${#request.getContextPath()}+'/static/css/thesame.css'" media="all">
    <link rel="stylesheet" th:href="${#request.getContextPath()}+'/static/css/rightCon.css'" media="all">
    <link rel="stylesheet" th:href="${#request.getContextPath()}+'/static/css/page.css'" media="all">
</head>
<body class="layui-layout-body layui-layout-body-scroll">
<div class="layui-body">
    <!-- 内容主体区域 -->
    <div class="scroll-box bgWhite box-shadows border-radius4">
        <div class="scroll-box-padding">
            <div class="scroll-box-con">
                <div class="border-bot1px pb10px mb20px">
                    <div class="form-box form-style">
                        <div>
                            <div class="form-title">
                                <label>设备名称：</label>
                            </div>
                            <div class="form-content">
                                <p>
                                    <input type="text" id="deviceName" placeholder="请输入">
                                </p>
                            </div>
                        </div>
                        <div>
                            <div class="form-title">
                                <label>设备状态：</label>
                            </div>
                            <div class="form-content">
                                <p>
                                    <select class="form-select-img" id="state">
                                        <option value="">请选择</option>
                                        <option value="1">在线</option>
                                        <option value="0">离线</option>
                                    </select>
                                </p>
                            </div>
                        </div>
                        <div class="form-box-btn overflow">
                            <span class="white border-radius4 border-blue blue inline-block pl20px pr20px lh30px cursor" onclick="queryPage();">查&nbsp;询</span>
                        </div>
                    </div>
                </div>
                <div>
                    <div class="mb20px lh32px overflow table-btn-img">
                        <p class="left">
                            <span class="white border-radius4 bgBlue-linear inline-block pl15px pr15px cursor" onclick="addDevice();"><img th:src="${#request.getContextPath()}+'/static/img/icon_add.png'">新建</span>
                        </p>
                        <p class="right">共<span class="red" id="total"></span>条数据</p>
                    </div>
                    <div class="border1px border-box border-radius4">
                        <table class="layui-hide" id="test" lay-filter="test"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:src="${#request.getContextPath()}+'/static/js/jquery-3.1.1.min.js'"></script>
<script th:src="${#request.getContextPath()}+'/static/layui/layui.js'" charset="utf-8"></script>
<script th:src="${#request.getContextPath()}+'/static/js/selectColor.js'" charset="utf-8"></script>
<script>
    $(function(){
        var deviceName = $("#deviceName").val();
        var state = $("#state").val();
        getPage(deviceName, state)
    })
    function queryPage(){
        var deviceName = $("#deviceName").val();
        var state = $("#state").val();
        getPage(deviceName, state)
    }

    function getPage(deviceName, state){
        layui.use(['element', 'table'], function() {
            var element = layui.element;
            var table = layui.table;
            //第一个实例
            table.render({
                elem: '#test'
                ,url: "../device/deviceList"
                , where: {
                    deviceName: deviceName, state: state
                }
                ,request: {
                     pageName: 'currentPage' //页码的参数名称，默认：page
                    ,limitName: 'nums' //每页数据量的参数名，默认：limit
                },
                page: true //开启分页
                , limit: 10 //每页显示的条数
                , groups: 9 //连续页码个数
                , parseData: function (res) {
                    // console.log(res);
                    $("#total").html(res.count);
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.count,
                        "data": res.data,
                    };
                }
                ,cols: [
                    [ //表头
                        {field: 'num', title: '序号', fixed: 'left',width: 80},
                        {field: 'deviceName', title: '设备名称',},
                        {field: 'address', title: '安装位置/地址',},
                        {field: 'deviceUid', title: '设备序列号',},
                        {field: 'assetNum', title: '固定资产编号',},
                        {field: 'equipModel', title: '设备型号',},
                        {field: 'status', title: '设备状态',
                            templet: function(res){
                                console.log(res);
                                if (res.status == 1) {
                                    return "<div class='table-list-btn'><span>在线</span></div>"
                                } else {
                                    return "<div class='table-list-btn'><span class=\"red\">离线</span></div>"
                                }
                            }
                        },
                        {field: 'operation', title: '操作', width: 160, fixed: 'right',
                            templet: function(res){
                                return "<div class='table-list-btn'><span class='blue' onclick='getDevice("+res.id+")'>详情</span><span class='blue' onclick='delDevice("+res.id+")'>删除</span></div>"
                            }
                        }
                    ]
                ]
            });
        });
    }

    function addDevice(){
        window.location.href="add";
    }
    
    function getDevice(id) {
        window.location.href="edit?id="+id;
    }

    function delDevice(id) {
        layer.confirm('确认要删除吗？', {
            btn: ['确定', '取消']
        }, function () {
            layer.closeAll();
            $.ajax({
                url: "../device/delete",
                type: "post",
                async: false,
                dataType: "json",
                data: {"id":id},
                success: function (result) {
                    if (result.code == 0) {
                        layer.alert("操作成功", function () {
                            window.location.href = "../device/list";
                        })
                    } else {
                        layer.msg(result.msg);
                    }
                }
            })
        });
    }
    

</script>
</body>
</html>
